package com.zjff.myshop.front.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjff.myshop.entity.User;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.front.pojo.vo.UserVO;
import com.zjff.myshop.front.service.UserService;
import com.zjff.myshop.mapper.UserMapper;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.MD5Util;
import com.zjff.myshop.util.PageList;
import com.zjff.myshop.util.PageQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录
     */
    @Override
//    public UserLoginVO login(String name, String password) {
    public User login(String name, String password) {
        User user = userMapper.selectByName(name);
        if (user == null) {
            throw new BusinessException("用户名不存在或密码错误");
        }

        String passwordMd5 = MD5Util.md5Encode(password + user.getSalt(), "UTF-8");
        if (passwordMd5.equals(user.getPassword())) {
            return user;
        }
        throw new BusinessException("用户登录错误");
    }

    /**
     * 用户登出
     */
    @Override
    public Boolean logout() {
        return true;
    }

    /**
     * 增加用户（注册）
     */
    @Override
    public Boolean insert(User user) {
        user.setId(null);
        return userMapper.insert(user) > 0;
    }

    /**
     * 删除单个
     */
    @Override
    public Boolean deleteById(Integer id) {
        //检查记录是否存在
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            throw new BusinessException("id对应的用户不存在");
        }

        return userMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 删除多个
     */
    @Override
    public Boolean deleteBatchByIds(Integer[] ids) {
        return userMapper.deleteBatch(ids) > 0;
    }

    /**
     * 更改用户信息
     */
    @Override
    public Boolean update(User user) {
        //检查参数
        Integer userId = user.getId();
        if (userId == null || userId <= 0) {
            throw new BusinessException("参数错误");
        }
        //更新
        return userMapper.update(user) > 0;
    }

    /**
     * 查询用户分页列表
     */
    @Override
    public PageList<UserVO> selectPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<User> list = userMapper.selectPage(pageQuery);
        int total = (int) new PageInfo<>(list).getTotal();
        List<UserVO> voList = BeanUtils.copyList(list, UserVO.class);
        return new PageList<>(voList, total, pageQuery.getLimit(), pageQuery.getPage());
    }

    /**
     * 查询用户(根据用户id)
     */
    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询用户(根据用户name)
     */
    public User selectByName(String name) {
        return userMapper.selectByName(name);
    }

    /**
     * 查询用户(根据用户phone)
     */
    public User selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

}
